package com.eshop.modules.activity.service.impl;

import java.lang.invoke.SerializedLambda;
import java.io.IOException;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import java.util.Iterator;
import java.util.Collection;
import java.util.Arrays;
import com.eshop.modules.product.domain.StoreProduct;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import cn.hutool.core.util.StrUtil;
import com.eshop.modules.activity.service.dto.StoreCouponDto;
import java.util.LinkedHashMap;
import java.util.List;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.activity.service.dto.StoreCouponQueryCriteria;
import com.eshop.modules.product.service.StoreProductService;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.activity.service.StoreCouponService;
import com.eshop.modules.activity.domain.StoreCoupon;
import com.eshop.modules.activity.service.mapper.StoreCouponMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class StoreCouponServiceImpl extends BaseServiceImpl<StoreCouponMapper, StoreCoupon> implements StoreCouponService
{
    private final IGenerator generator;
    private final StoreProductService storeProductService;
    
    public Map<String, Object> queryAll(final StoreCouponQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<StoreCoupon> page = (PageInfo<StoreCoupon>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        final List<StoreCouponDto> storeCouponDtos = (List<StoreCouponDto>)this.generator.convert(page.getList(), (Class)StoreCouponDto.class);
        for (final StoreCouponDto storeCouponDto : storeCouponDtos) {
            if (StrUtil.isNotBlank((CharSequence)storeCouponDto.getProductId())) {
                final List<StoreProduct> storeProducts = (List<StoreProduct>)(this.storeProductService.lambdaQuery().in(StoreProduct::getId, (Collection)Arrays.asList(storeCouponDto.getProductId().split(",")))).list();
                storeCouponDto.setProduct(storeProducts);
            }
        }
        map.put("content", storeCouponDtos);
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<StoreCoupon> queryAll(final StoreCouponQueryCriteria criteria) {
        return (List<StoreCoupon>)((StoreCouponMapper)this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate(StoreCoupon.class, criteria));
    }
    
    public void download(final List<StoreCouponDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final StoreCouponDto yxStoreCoupon : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("优惠券名称", yxStoreCoupon.getTitle());
            map.put("兑换消耗积分值", yxStoreCoupon.getIntegral());
            map.put("兑换的优惠券面值", yxStoreCoupon.getCouponPrice());
            map.put("最低消费多少金额可用优惠券", yxStoreCoupon.getUseMinPrice());
            map.put("优惠券有效期限（单位：天）", yxStoreCoupon.getCouponTime());
            map.put("排序", yxStoreCoupon.getSort());
            map.put("状态（0：关闭，1：开启）", yxStoreCoupon.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public StoreCouponServiceImpl(final IGenerator generator, final StoreProductService storeProductService) {
        this.generator = generator;
        this.storeProductService = storeProductService;
    }
}
